var $ajaxForm;
var staffRid;
$(function() {
	initForm();
	staffRid = $("#staffRid").val();
	if(!(staffRid==null || staffRid == "")){
		
		initToolBar("footerWrap");
		$("#button").hide();
	}else{
		$("#footerWrap").hide();
		$("#button").show();
		staffRid = $($($(window.parent.document).find('#contetFrame')[0].contentWindow.document)
				.find('#staffRid')).val();
	}
	$("#div_btn_generatePDF").hide();
	
	WebUtils.setComboValue("reportFormat","HTML");
});

var initForm = function() {
	var options = {
		voType : generateStaffProfileVo,
		autoFillVo : false
	};
	$ajaxForm = $("#contentForm").AjaxForm(options);
};

var pimStaffProfileIdsVo = null;

var generatePDF_click = function(){
	var passWordValue=null;
	initpxml("div_generate_html");
	$("#div_generate_html iframe").each(objs2pxml);
	var pxml_html= getpxml();
	DwrHtml2PDF.convertHtml2Pdf(pxml_html,passWordValue,{
		callback : function(rtnData) {
			dwr.engine.setAsync(false);
			dwr.engine.openInDownload(rtnData);
		},
		exceptionHandler : function(e) {
			alert("Export Fail!");
		}
	});
};
var generate_click = function() {
	var obj = $ajaxForm.GetVo();
	
	if(obj.reportFormat != null && obj.reportFormat=="HTML"){
		if (!(staffRid == null || staffRid == "")) {
			var $div = $("#div_generate_html");
			$div.html("");
			generationHtml(obj, staffRid);
			parentFrameHeight();
			// hideToolBar();
			$("#div_btn_generateDialog").hide();
			$("#div_btn_generatePDF").show();
			
		}
		
	}else if(obj.reportFormat != null && obj.reportFormat !=""){
		if (!(staffRid == null || staffRid == "")) {
						
			var paramMap = new Object();
			paramMap["reportId"] = "StaffReport";
			paramMap["reportName"] = "StaffReport";
			paramMap["export_file"] = "StaffReport";
			paramMap["jasper_name"] = "StaffReport.jasper";
			paramMap["reportType"] = "PDF";
			paramMap["staffRid"] = staffRid;
			
			DwrPIMRPT.exportAtt(paramMap,3, {
				callback : function(rtnData) {
					dwr.engine.setAsync(false);
					dwr.engine.openInDownload(rtnData);
				},
				exceptionHandler : function(e) {
					alert("Export Fail!");
				}
			});
		}
			//window.location="../common/ExportFile.jsp?format="+obj.reportFormat;
		var $btn_generateDialog = $("#btn_generateDialog");
		$btn_generateDialog.hide();
	}
};

var generationHtml = function(obj,staffRid){
	var url = null;
	DwrGenerateStaffProfile.searchByStaffRid(staffRid, function(data) {
		if (data.status == 1) {
			dwr.engine.setAsync(false);
			pimStaffProfileIdsVo = data.object;
			for ( var staffName in obj) {
				var staffNameValue = obj[staffName];
				if (staffNameValue != null && staffNameValue == "Y") {
					if (staffName == "personalParticulars") {
						url = "personalParticularDetails.jsp";
						postHtml("personalParticulars", url, staffRid);
					} else if (staffName == "childrenParticulars") {
						url = "childrenParticulars.jsp";
						postHtml("childrenParticulars", url, staffRid,"&childID=");
					}else if (staffName == "officeInformation") {
						url = "editOfficeInfo.jsp";
						postHtml("officeInformation", url, staffRid);
					} else if (staffName == "actingRecord") {
						url = "actingRecord.jsp";
						postHtml("actingRecord", url, staffRid, "&actingId=");
					}else if (staffName == "trainingRecord") {
						url = "trainingRecordDisplay.jsp";
						postHtml("trainingRecord", url, staffRid,
								"&trainingRecordId=");
					}else if (staffName == "qualificationRecord") {
						url = "qualificationsRecord.jsp";
						postHtml("qualificationRecord", url, staffRid,
								"&qualificationId=");
					} else if (staffName == "appraisalRecord") {
						url = "appraisalRecordDetails.jsp";
						postHtml("appraisalRecord", url, staffRid, "&appraisalId=");
					}  else if (staffName == "appointmentHistory") {
						url = "appointmentRecordDetails.jsp";
						postHtml("appointmentHistory", url, staffRid,
								"&appointmentRid=");
					}else if (staffName == "disciplinaryRecord") {
						url = "disciplinaryRecordDetails.jsp";
						postHtml("disciplinaryRecord", url, staffRid, "&disciplinaryId=");
					} else if (staffName == "spouseParticulars") {
						url = "spouseParticulars.jsp";
						postHtml("spouseParticulars", url, staffRid,
								"&spouseId=");
					} else if (staffName == "relativeParticulars") {
						url = "relativeParticulars.jsp";
						postHtml("relativeParticulars", url, staffRid,
								"&relativeId=");
					}else if (staffName == "postingRecord") {
						url = "postingRecordDetails.jsp";
						postHtml("postingRecord", url, staffRid, "&postingId=");
					}else if (staffName == "promotionRecord") {
						url = "promotionRecordDetails.jsp";
						postHtml("promotionRecord", url, staffRid, "&promotionId=");
					}  else if (staffName == "mentorRecord") {
						url = "mentorshipRecordList.jsp";
						postHtml("mentorRecord", url, staffRid);
					}  else if (staffName == "unionRecord") {
						url = "unionRecord.jsp";
						postHtml("unionRecord", url, staffRid,"&unionId=");
					}  else if (staffName == "honourAndAwards") {
						url = "honoursRecordList.jsp";
						postHtml("honourAndAwards", url, staffRid,"&honourId=");
					}else if (staffName == "contactInformation") {
						url = "contactInfoDetails.jsp";
						postHtml("contactInformation", url, staffRid);
					} else if (staffName == "separationRecord") {
						url = "separationRecord.jsp";
						postHtml("separationRecord", url, staffRid,"&separationId=");
					} 
				}
			}
			/*$(document).ready(
				attachFrame()
			);*/
			
		} else {
			// fail, show error
			if (typeof (data.messages) != "undefined" && data.messages != null
					&& data.messages != "") {
				// WebUtils.showTextMessage(data.messages);
				showMsgBox(1, data.messages, "");
			}
		}
		
		dwr.engine.setAsync(true);
	});
};

var attachFrame = function() {
	dwr.engine.setAsync(false);
	var frames = $("#div_generate_html").find("iframe");
	if (typeof (frames) != "undefined" && frames != null) {
		for ( var i = 0; i < frames.length; i++) {
			var ff = frames[i];
			if (typeof (ff) != "undefined" && ff != null) {
				$("button", ff.contentWindow.document).hide();
				$("a", ff.contentWindow.document).hide();
				$("div[class='button secondaryBtn']",ff.contentWindow.document).hide();
			}
		}
	}
	dwr.engine.setAsync(true);
};

/*
 * var postHtml = function(PimFrameName, pageName, staffRid, otherParams) { url =
 * g_contextPath + "/modules/pim/pages/" + pageName; var pimVoList =
 * pimStaffProfileIdsVo[PimFrameName]; if (pimVoList != null && pimVoList.length >
 * 0) { for ( var i in pimVoList) { var vo = pimVoList[i]; if (vo != null) {
 * createIframe(PimFrameName+i, url,vo); } } } };
 */

var postHtml = function(PimFrameName, pageName, staffRid, otherParams) {
	url = g_contextPath + "/modules/pim/pages/" + pageName + "?staffRid="
			+ staffRid + "&recordStatus=ACT&isGenerateHtml=Y";
	if (typeof (otherParams) != "undefined" && pimStaffProfileIdsVo != null) {
		var pimVoList = pimStaffProfileIdsVo[PimFrameName];
		if (pimVoList != null && pimVoList.length > 0) {
			for ( var i in pimVoList) {
				var vo = pimVoList[i];
				if (vo != null) {
					for ( var j in vo) {
						var id = vo[j];
						if (id != null) {
							var params = otherParams + id;
							var paramsUrl = url + params;
							createIframe(PimFrameName + i, paramsUrl);
						}
					}
				}
			}
		}
	} else {
		createIframe(PimFrameName+"_Frame", url);
	}
};

var createIframe = function(PimFrameName, url, vo) {
	var $iframe = $('<iframe id="'
			+ PimFrameName
			+ '" name="'
			+ PimFrameName
			+ '" scrolling="no" frameborder=0 onLoad="iFrameHeight(this.id)" style="width: 100%;"></iframe>');
	var $div = $("#div_generate_html");
	$iframe.appendTo($div);
	$iframe.attr("src", url);
	
	//$("table",$iframe.contentWindow.document).hide();
};


/*
 * var createIframe = function(PimFrameName, url,vo) { var $iframe = $('<iframe
 * id="' + PimFrameName + '" name="' + PimFrameName + '" scrolling="no"
 * frameborder=0 onLoad="iFrameHeight(this.id)" style="width: 100%;"></iframe>');
 * var $div = $("#div_generate_html"); $iframe.appendTo($div);
 * $iframe.attr("src", url); $.post(url,{}, function(data) { openAdditional(); //
 * top.$('#dlg_searchAndSelectStaff').append(data);
 * top.$('#dlg_searchAndSelectStaff').html(data);
 * $("#btn_add_Additional_Staff_Will_Be_Notified") .die("click");
 * $("#btn_add_Additional_Staff_Will_Be_Notified") .live("click",
 * openAdditional);
 * 
 * });
 * 
 * var $ajaxFormPage = $($iframe[0].contentWindow.$ajaxForm);
 * $ajaxFormPage.FillForm(vo, $ajaxFormPage[0]); };
 */

var resetExportHtmlHeight=function(){
	dwr.engine.setAsync(false);
		for ( var i = 0; i < window.frames.length; i++) {
			 var ff = window.frames[i].frameElement.id;
			iFrameHeight(ff);
		}
    dwr.engine.setAsync(true);
};

var iFrameHeight = function(iframeId) {
	var ifm = document.getElementById(iframeId);
	var subWeb = document.frames ? document.frames[iframeId].document
			: ifm.contentDocument;
	if (ifm != null && subWeb != null) {
		ifm.height = subWeb.body.scrollHeight;
	}
};

var parentFrameHeight = function(){
	var $ifm = $(window.parent.document).find('#pimContetFrame');
	$ifm.height = document.body.offsetHeight;
	$ifm.scrolling = "no";
};

var pack_click = function() {
	//var obj = $ajaxForm.GetVo();
	var staffRid = $("#staffRid").val();
	DwrGenerateStaffProfile.packStaffProfile(staffRid,function(data){
		if(data.status==1){
			alert(1111);
		}
	});
};

var unpack_click = function() {
	//var obj = $ajaxForm.GetVo();
	var staffRid = $("#staffRid").val();
	DwrGenerateStaffProfile.unpackStaffProfile(staffRid,function(data){
		if(data.status==1){
			alert(1111);
		}
	});
};

var closeDialog_click=function(){
	showToolBar();
	window.parent.$.unblockUI();
};

/**
 * close click
 * 
 * @returns
 */
var close_click = function() {
	/*
	 * var obj = $ajaxForm.GetVo(); var blog = false; for ( var i in obj) { if
	 * (obj[i] != null && obj[i] != "") { blog = true; } } if (blog) { if
	 * (confirm(MsgRepo["msg.common006"].text)) { history.back(); } else {
	 * return; } }
	 */
	history.back();
};